<template src="./template.html"></template>

<style src="./style.css" scoped></style>

<script>
    import positionIcon from 'assets/images/position-icon.png';
    import couponActiveBg from './images/coupon-bg.png';
    import couponUnActiveBg from './images/coupon-gray-bg.png';
    import couponUseIcon from './images/coupon-use-icon.png';
    import couponCostIcon from './images/coupon-cost-icon.png';
    import couponDeadIcon from './images/coupon-due-icon.png';
    import api from 'api/api';
    import navigator from 'utils/navigator';
    import route from 'constants/route';
    import constant from 'constants/constant';
    import modal from 'utils/modal';

    const globalEvent = weex.requireModule('globalEvent');

    const sortTabs = [
        {
            type: 1,
            title: '未使用'
        },
        {
            type: 2,
            title: '使用记录'
        },
        {
            type: 3,
            title: '已过期'
        }
    ];

    function vendorData(data, vm){
        data = data || {};
        vm.unUsedCount = data.unused_count || 0;
        vm.usedCount = data.used_count || 0;
        vm.outDateCount = data.outdate_count || 0;
        vm.couponList = data.cdata || [];
    }

    export default{
        data(){
            return{
                positionIcon: positionIcon,
                couponActiveBg: couponActiveBg,
                couponUnActiveBg: couponUnActiveBg,
                couponUseIcon: couponUseIcon,
                couponCostIcon: couponCostIcon,
                couponDeadIcon: couponDeadIcon,
                sortTabs: sortTabs,
                type: 1, //优惠券类型 默认1(未使用)
                couponValue: '', //优惠券code
                unUsedCount: 0, //未使用优惠券的数量
                usedCount: 0, //使用过优惠券的数量
                outDateCount: 0, //过期优惠券的数量
                couponList: [], //优惠券列表
                pageLoaded: false //页面是否加载
            }
        },
        created(){
            let self = this;
            self.sendHttp();


            //监听优惠券生成订单事件
            globalEvent.addEventListener(constant.EVENT_COUPON_TO_ORDER, function (e) {
                self.initData();
                self.sendHttp();
            });
        },
        methods: {
            sendHttp(){
                let self = this;
                api.sendHttp('getMyCouponApi', {
                    type: self.type
                }).then(res => {
                    self.pageLoaded = true;
                    vendorData(res.data, self);
                }, res => {
                    self.pageLoaded = true;
                    modal.toast(res.msg || '获取优惠券失败');
                });
            },
            initData(){
                this.pageLoaded = false;
                this.couponList.length = 0;//清空数组
            },
            sortClick(s) {
                let self = this;
                //if(s && s.type == self.type) return false;
                self.type = s.type;
                self.initData();
                this.sendHttp();
            },
            updateCouponList(c){
                let self = this;
                //将最新的数据插入到数组头部
                if(c && c.length) Array.prototype.unshift.apply(self.couponList, c);
                self.unUsedCount = parseInt(self.unUsedCount) + c.length;
            },
            bindCoupon(){ //绑定
                let self = this;
                if(self.checkInput()){
                    let csno = self.couponValue.replace(/\s/g,'');
                    api.sendHttp("bindCouponApi",{csno: csno}).then(res=>{
                        modal.toast("绑定成功");
                        self.updateCouponList(res.data);
                        self.couponValue = ''; //重置优惠券
                        this.$refs['coupon_input'].blur(); //隐藏键盘
                    },res=>{
                         modal.toast(res.msg || "绑定优惠券失败");
                        this.$refs['coupon_input'].blur(); //隐藏键盘
                    });
                }
            },
            restCouponVal(){ //重输
                this.couponValue = '';
            },
            userCoupon(coupon){ //使用
                if(this.type == 2 || this.type == 3) { //pass掉已使用、已过期的优惠券
                    return false;
                }
                let jtype = coupon.jtype;
                if(jtype == 1) { //店铺
                    if(coupon.shoptype){ //大店铺shoptype
                        let _options = {
                            url: route.shop_index,
                            title: coupon.shopname || "商家店铺",
                            params: {
                                shopType: coupon.shoptype
                            }
                        };
                        navigator.push(_options);
                    } else {  //小店铺sid
                        let _options = {
                            url: route.shop_plus,
                            title: coupon.shopname || "商家店铺",
                            params: {
                                sid: coupon.sid
                            }
                        };
                        navigator.push(_options);
                    }
                }

                if(jtype == 2) { //单商品
                    let options = {
                        url: route.detail_index,
                        title: '商品详情',
                        params: {
                            id: coupon.gid
                        }
                    };
                    navigator.push(options);
                }

                if(jtype == 3) { //多商品
                    console.log("多商品情况");
                }
            },
            checkInput(){
                let self = this;
                if(!self.couponValue) {
                    modal.toast("请输入优惠券兑换码");
                    return false;
                }
                if(self.couponValue.replace(/\s/g,'').length != 12) {
                    modal.toast("优惠券兑换码长度有误");
                    return false;
                }
                if(!/^[0-9a-zA-Z]*$/g.test(self.couponValue.replace(/\s/g,''))) {
                    modal.toast("优惠券码只能输入数字或字母");
                    return false;
                }
                return true;
            },
            oninput(event){
                let self = this;
                let v = event.value;
                if(/\S{5}/.test(v)){
                    self.couponValue = v.replace(/\s/g, '').replace(/(.{4})/g, '$1 ').substr(0,14);
                }
            }
        },
        destroyed(){
            globalEvent.removeEventListener(constant.EVENT_COUPON_TO_ORDER);
        }
    }
</script>
